﻿using System;
using KillBill.Business.Entities;
using KillBill.Presentation.Common;
using KillBill.Presentation.Modules.Administration.AdministrationServiceReference;
using KillBill.Presentation.Modules.Administration.IView;
using KillBill.Services.DTO;

namespace KillBill.Presentation.Modules.Administration.Presenter
{
	public class EmployeeAddEditPresenter
	{
        public IEmployeeAddEdit View { get; set; }

        private readonly AdministrationServiceClient _service;

        public EmployeeAddEditPresenter(IEmployeeAddEdit view)
        {
            _service = new AdministrationServiceClient();
            View = view;
            View.EmployeeRoleList = _service.GetAplicactionRoleList(Context.SessionID).Roles;
            View.IsEditing = false;
        }

        public EmployeeAddEditPresenter(IEmployeeAddEdit view, Employee employee)
        {
            _service = new AdministrationServiceClient();
            View = view;
            View.EmployeeRoleList = _service.GetAplicactionRoleList(Context.SessionID).Roles;
            View.IsEditing = true;
        }

        public void Save()
        {
            View.Employee.Password = View.Password;
            KillBillDTO dto = View.IsEditing ? 
                _service.UpdateEmployee(View.Employee, Context.SessionID) : 
                _service.AddEmployee(View.Employee, Context.SessionID);
            View.ShowMessage((dto.Success) ? 
                dto.Messages[0] : dto.Messages[0] + " " + 
                dto.Messages[1]);
        }
	}
}
